package com.zyd.blog.controller;

import com.github.pagehelper.PageInfo;
import com.zyd.blog.business.enums.ResponseStatus;
import com.zyd.blog.business.service.BizSmalltalktypeService;
import com.zyd.blog.business.vo.SmalltalktypeVO;
import com.zyd.blog.framework.object.PageResult;
import com.zyd.blog.framework.object.ResponseVO;
import com.zyd.blog.persistence.beans.BizSmalltalktype;
import com.zyd.blog.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 说说-类别-控制器
 */
@RestController
@RequestMapping("/Smalltalktypes")
public class SmalltalktypesController {


    @Autowired
    BizSmalltalktypeService bizSmalltalktypeService;

    // 测试
    @RequestMapping("/test")
    public String test(String name){
        return "Hello "+name;
    }

    // 列表
    @PostMapping("/list")
    public PageResult list(SmalltalktypeVO vo) {
        PageInfo<BizSmalltalktype> pageInfo = bizSmalltalktypeService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }

    //

    // 类别添加
    @PostMapping(value = "/add")
    public ResponseVO add(BizSmalltalktype bizSmalltalktype) {
        bizSmalltalktypeService.insert(bizSmalltalktype);
        return ResultUtil.success("说说类别添加成功！新类别 - " + bizSmalltalktype.getName());
    }

    // 删除
    @PostMapping(value = "/remove")
    public ResponseVO remove(Long[] ids) {
        if (null == ids) {
            return ResultUtil.error(500, "请至少选择一条记录");
        }
        for (Long id : ids) {
            bizSmalltalktypeService.removeByPrimaryKey(id);
        }
        return ResultUtil.success("成功删除 [" + ids.length + "] 个说说类别");
    }


    //查询单个
    @PostMapping("/get/{id}")
    public ResponseVO get(@PathVariable Long id) {
        return ResultUtil.success(null, this.bizSmalltalktypeService.getByPrimaryKey(id));
    }

    // 修改
    @PostMapping("/edit")
    public ResponseVO edit(BizSmalltalktype bizSmalltalktype) {
        try {
            bizSmalltalktypeService.updateSelective(bizSmalltalktype);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("说说-类别-修改失败！");
        }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }





}
